package com.easondongh.blog.controller;

import com.easondongh.blog.enums.ResultEnum;
import com.easondongh.blog.pojo.Type;
import com.easondongh.blog.service.TypeService;
import com.easondongh.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 博客类别
 * @author EasonDongH
 * @date 2020/3/26 19:39
 */
@RestController
@RequestMapping("type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    /**
     * 新增博客分类
     * @param type
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<Object> save(@RequestBody Type type) {
        this.typeService.save(type);
        return new Result<>(ResultEnum.SUCCESS);
    }

    /**
     * 后台查询所有，包含弃用的
     * @return
     */
    @RequestMapping(value = "/listBack", method = RequestMethod.GET)
    public Result<List<Type>> list(){
        List<Type> list = this.typeService.getAll();
        return new Result<>(list);
    }

    /**
     * 后台查询所有，不包含弃用的
     * @return
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public Result<List<Type>> getList() {
        List<Type> list = this.typeService.getTypeList();
        return new Result<>(list);
    }

    /**
     * 更新分类
     * @param type
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public Result<Object> update(@RequestBody Type type) {
        this.typeService.update(type);
        return new Result<>(ResultEnum.SUCCESS);
    }

    /**
     * 根据id查询博客分类
     * @param id
     * @return
     */
    @RequestMapping(value = "get/{id}", method = RequestMethod.GET)
    public Result<Type> getById(@PathVariable Integer id) {
        Type type = this.typeService.getById(id);
        return new Result<>(type);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result<Object> delete(@PathVariable Integer id) {
        this.typeService.deleteById(id);
        return new Result<>(ResultEnum.SUCCESS);
    }

    /**
     * 根据id启用
     * @param id
     * @return
     */
    @RequestMapping(value = "/enable/{id}", method = RequestMethod.PUT)
    public Result<Object> enable(@PathVariable Integer id) {
        this.typeService.enable(id);
        return new Result<>(ResultEnum.SUCCESS);
    }

    /**
     * 根据id弃用
     * @param id
     * @return
     */
    @RequestMapping(value = "/disable/{id}", method = RequestMethod.PUT)
    public Result<Object> disable(@PathVariable Integer id) {
        this.typeService.disable(id);
        return new Result<>(ResultEnum.SUCCESS);
    }
}
